package com.xiaoshu.aop;

import com.xiaoshu.disruptor.MessageDisruptorQueue;
import com.xiaoshu.disruptor.MethodInfoModel;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.lang.reflect.Method;

/**
 * 功能说明：对指定注解进行拦截，本切面是对积分注解进行切面处理；
 * com.xiaoshu.aop
 *
 * <p>
 * Original @Author: deane.jia-贾亮亮,2021/6/15 11:27
 * <p>
 * Copyright (C)2012-2021 小树盛凯科技 All rights reserved.
 */
@Component
@Aspect
@Slf4j
public class LevelScoreAspect {

    @Pointcut("@annotation(com.xiaoshu.aop.LevelRecordAnnotation)")
    private void anyMethod() {
        // define the Pointcut
    }

    /**
     * 拦截执行方法，并记录积分数据，并发送数据到前端
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around(value = "anyMethod()")
    public Object doExecute(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("[Any method] start Aspect .... :{}", joinPoint.toString());
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        Method method = methodSignature.getMethod();
        // put the method info and  parameters to Queue;
        MessageDisruptorQueue.publishEvent(new MethodInfoModel(method, joinPoint.getArgs()));
        Object proceed = joinPoint.proceed();
        return proceed;
    }
}
